package cn.smartgarden.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.smartgarden.model.Args;
import cn.smartgarden.service.ArgsService;
import cn.smartgarden.service.Utility;

@Controller
@RequestMapping("/args")
public class ArgsController {
@Autowired
private ArgsService argsService;

@RequestMapping(value="/getAll",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getAll(){
	Map<String, Object> ret=new HashMap<>();
	List<Args> args=argsService.getAll();
	if (args!=null) {
		ret.put("status", 1001);
		ret.put("body", args);
	}
	else {
		ret.put("status", 1002);
	}
	return Utility.gson.toJson(ret);
}
@RequestMapping(value="/insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String insert(@RequestBody Args args){
	Map<String, Object> ret=new HashMap<>();
	if (argsService.insert(args)==1) {
		ret.put("status", 1001);
	}
	else {
		ret.put("status", 1002);
	}
	return Utility.gson.toJson(ret);
}

@RequestMapping(value="/getByDay",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String getByDay(@RequestBody Map<String, Object> args){
	String day=(String) args.get("day");
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	Map<String, Object> ret=new HashMap<>();
	Date date=null;
	try {
		date=df.parse(day);
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	List<Args> args2=argsService.getByDay(date);
	if (args2!=null) {
		ret.put("status", 1001);
		ret.put("body", args2);
	}
	else {
		ret.put("status", 1002);
	}
	return Utility.gson.toJson(ret);
}

@RequestMapping(value="/getSbh",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getSbh(){
	Map<String, Object> ret=new HashMap<>();
	List<Map<String, Object>> sbhs=argsService.getSbh();
	if (sbhs!=null) {
		ret.put("status", 1001);
		ret.put("body", sbhs);
	}
	else {
		ret.put("status", 1002);
	}
	return Utility.gson.toJson(ret);
}

@RequestMapping(value="/getByDaySbh",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String getByDaySbh(@RequestBody Map<String, Object> args){
	String day=(String)args.get("day");
	String sbh=(String)args.get("sbh");
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	Map<String, Object> ret=new HashMap<>();
	Date date=null;
	try {
		date=df.parse(day);
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	List<Args> args2=argsService.getByDateSbh(date, sbh);
	if (args2!=null) {
		ret.put("status", 1001);
		ret.put("body", args2);
	}
	else {
		ret.put("status", 1002);
	}
	return Utility.gson.toJson(ret);
}
@RequestMapping(value="/getLastRecord",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String getLastRecord(@RequestBody Map<String, Object> args){
	Integer num=Integer.parseInt((String)args.get("num"));
	String sbh=(String) args.get("sbh");
	List<Args> args2=argsService.getLastRecord(num, sbh);
	Map<String, Object> ret=new HashMap<>();
	if (args2!=null) {
		ret.put("status", 1001);
		ret.put("body", args2);
	}
	else {
		ret.put("status", 1002);
	}
	return Utility.gson.toJson(ret);
}
}
